# -*- coding: utf-8 -*-
import traceback
from utils.backcode import ImportError
from app.manage.financeManage.common.manage_sql import FinanceManageSql
from utils.logger import Md_Debug
from utils.db_utils import DbConn
import datetime


class FinanceManageDB(object):
    """视频"""
    
    def __init__(self):
        self.db = DbConn()
        self.now = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d")
        self.time = datetime.datetime.strftime(datetime.datetime.now(), "%Y-%m-%d %H:%M:%S")
    
    def finance_list(self, body):
        """任务列表"""
        try:
            ret = dict()
            db = DbConn()
            page = body.get('page', None)
            size = body.get('size', None)
            
            # check parameter
            if not page:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "page is none"
            elif not size:
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "size is none"
            else:
                list_data = db.fetch_all(FinanceManageSql.FINANCE_LIST)
                page_list = FinanceManageDB.paging(self, list_data, page, size)
                code = ImportError.IMPORT_CODE_SUCCESS
                message = "success"
                ret['response_data'] = page_list
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret
    
    
    def finance_info(self, body):
        """查看任务详情信息"""
        try:
            ret = dict()
            finance_id = body.get("finance_id")
            report_classfy_id = body.get("report_classfy_id")
            
            # check parameter
            if not str(finance_id):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "finance_id is none"
            elif not str(report_classfy_id):
                code = ImportError.IMPORT_CODE_INVALID_ARGS
                message = "report_classfy_id is none"
            else:
                back_one = self.db.fetch_all(FinanceManageSql.FINANCE_INFO, [finance_id])
                back_list = self.db.fetch_all(FinanceManageSql.FINANCE_REPORT, [report_classfy_id, finance_id])
                code = ImportError.IMPORT_CODE_SUCCESS
                message = "success"
                ret['finance_info'] = back_one
                ret['finance_report'] = back_list
        except Exception as e:
            code = ImportError.IMPORT_CODE_EXCEPTION
            message = str(e)
            Md_Debug.error(traceback.format_exc(e))
            print(traceback.format_exc(e))
        ret['response_code'] = code
        ret['response_msg'] = message
        return ret
    
    def paging(self, dataList, page, size):
        """分页"""
        ret = {
            'page': page, 'size': size, 'total_page': 0, 'total_size': 0
        }
        total_size = len(dataList)
        if total_size % size == 0:
            total_page = total_size / size
        else:
            total_page = total_size / size + 1
        start_index = (page - 1) * size
        end_index = page * size
        ret['total_page'] = total_page
        ret['total_size'] = total_size
        if len(dataList) < end_index:
            ret['data'] =  dataList[start_index:]
        else:
            ret['data'] = dataList[start_index: end_index]
        return ret
